package com.example.base.app;

import com.example.base.security.SecurityUtil;
import com.example.base.security.SysUser;
import com.example.base.security.SysUserType;
import com.example.base.security.domain.SysUserToken;
import com.example.base.security.jwt.JWTConfig;
import com.example.base.security.jwt.JWTTokenUtil;
import com.example.base.security.service.SysUserTokenService;
import com.example.base.system.domain.OpenUserToken;
import com.example.base.system.service.OpenUserTokenService;
import com.example.base.utils.ResponseUtils;
import com.example.base.utils.Result;
import com.example.base.utils.SpringContextUtils;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.Jwts;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

/**
 * app 登录拦截
 */
@Component
@Slf4j
public class AppLoginHandler {

    @Autowired
    private OpenUserTokenService openUserTokenService;

    public boolean login(HttpServletRequest request, HttpServletResponse httpServletResponse,String appName){
        try {
            String openToken=request.getHeader("openToken");
            if(StringUtils.isNotBlank(openToken)){
                OpenUserToken openUserToken=openUserTokenService.valited(openToken);
                if(openUserToken!=null){
                    SysUser sysUser=new SysUser();
                    sysUser.setUserId(openUserToken.getUserid());
                    sysUser.setUserName(openUserToken.getUsername());
                    sysUser.setUserType(SysUserType.OPEN.getUserType());
                    SecurityUtil.setSysUserEntity(sysUser);
                    return true;
                }
            }
            String token=request.getHeader("Authorization");
            if(StringUtils.isNotBlank(token)){
                token = token.replace(JWTConfig.tokenPrefix, "");

                // 解析JWT
                Claims claims = Jwts.parser()
                        .setSigningKey(JWTConfig.secret)
                        .parseClaimsJws(token)
                        .getBody();
                // 使用签发时间
                Date date=claims.getIssuedAt();
                Date current=new Date();
                log.info("有效期{}",current.getTime() - date.getTime());
                //现在的时间减去签发时间
                if((current.getTime() - date.getTime()) > 10000){
                    SysUserTokenService userTokenService= SpringContextUtils.getBean(SysUserTokenService.class);
                    SysUserToken userToken=userTokenService.getMuserTokenByToken(JWTConfig.tokenPrefix+token);
                    if(userToken != null && userToken.getIsEnable()){
                        JWTTokenUtil.tokenPass(token,claims,httpServletResponse);
                        return true;
                    }
                }else {
                    JWTTokenUtil.tokenPass(token,claims,httpServletResponse);
                    return true;
                }
            }

        }catch (ExpiredJwtException e){
            e.printStackTrace();
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }
}
